package com.automapper.execute.impl;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.reflection.MetaObject;

import com.automapper.execute.Execute;
import com.automapper.executor.AutoMapperBatchExecutor;
import com.automapper.sqlbuilder.SqlBuilderTemplate;
import com.automapper.utils.PropertiesFacade;

public class UpdateRouterExecute implements Execute {
	
	private SqlBuilderTemplate sqlBuilder;

	@Override
	public void process(MetaObject metaStatementHandler, Invocation invocation, PropertiesFacade propertiesFacade) throws Exception {
		Executor executor = (Executor) metaStatementHandler.getValue("delegate.executor");
		Execute execute;
		if (executor instanceof AutoMapperBatchExecutor) {
			execute = new UpdateBatchExecute();
		} else {
			execute = new BaseExecute();
		}
		
		FieldUtils.writeDeclaredField(execute, "sqlBuilder", sqlBuilder, true);
		execute.process(metaStatementHandler, invocation, propertiesFacade);
	}
}
